﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

#endif

namespace StyleMVVM.View
{
	public class NavigatingPage : Page, INavigatingPage
	{
		/// <summary>
		/// Invoked as an event handler to navigate backward in the page's associated
		/// <see cref="Frame"/> until it reaches the top of the navigation stack.
		/// </summary>
		/// <param name="sender">Instance that triggered the event.</param>
		/// <param name="e">Event data describing the conditions that led to the event.</param>
		public virtual void GoHome(object sender, RoutedEventArgs e)
		{
#if NETFX_CORE
			// Use the navigation frame to return to the topmost page
			if (this.Frame != null)
			{
				while (this.Frame.CanGoBack)
				{
					this.Frame.GoBack();
				}
			}
#else
			if (NavigationService != null)
			{
				while (NavigationService.CanGoBack)
				{
					NavigationService.GoBack();
				}
			}
#endif
		}

		/// <summary>
		/// Invoked as an event handler to navigate backward in the navigation stack
		/// associated with this page's <see cref="Frame"/>.
		/// </summary>
		/// <param name="sender">Instance that triggered the event.</param>
		/// <param name="e">Event data describing the conditions that led to the
		/// event.</param>
		public virtual void GoBack(object sender, RoutedEventArgs e)
		{
#if NETFX_CORE
			// Use the navigation frame to return to the previous page
			if (this.Frame != null && this.Frame.CanGoBack)
			{
				this.Frame.GoBack();
			}
#else
	// Use the navigation frame to return to the previous page
			if (NavigationService != null && NavigationService.CanGoBack)
			{
				NavigationService.GoBack();
			}
#endif
		}

		/// <summary>
		/// Invoked as an event handler to navigate forward in the navigation stack
		/// associated with this page's <see cref="Frame"/>.
		/// </summary>
		/// <param name="sender">Instance that triggered the event.</param>
		/// <param name="e">Event data describing the conditions that led to the
		/// event.</param>
		public virtual void GoForward(object sender, RoutedEventArgs e)
		{
#if NETFX_CORE
			// Use the navigation frame to move to the next page
			if (this.Frame != null && this.Frame.CanGoForward)
			{
				this.Frame.GoForward();
			}
#else
	// Use the navigation frame to move to the next page
			if (NavigationService != null && NavigationService.CanGoForward)
			{
				NavigationService.GoForward();
			}
#endif
		}

#if !DOT_NET

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			if (NavigatedTo != null)
			{
				NavigatedTo(this, e);
			}

			base.OnNavigatedTo(e);
		}

		protected override void OnNavigatedFrom(NavigationEventArgs e)
		{
			if (NavigatedFrom != null)
			{
				NavigatedFrom(this, e);
			}

			base.OnNavigatedFrom(e);
		}

		protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
		{
			if (NavigatingFrom != null)
			{
				NavigatingFrom(this, e);
			}

			base.OnNavigatingFrom(e);
		}

#endif
		public event EventHandler<NavigationEventArgs> NavigatedTo;

		public event EventHandler<NavigationEventArgs> NavigatedFrom;

		public event EventHandler<NavigatingCancelEventArgs> NavigatingFrom;
	}
}